/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canteen.sys.view.individual;

import canteen.sys.SessionFactoryClass;
import canteen.sys.model.entity.BillDetail;
import canteen.sys.model.entity.BillMaster;
import canteen.sys.model.entity.EmployeeMaster;
import canteen.sys.model.entity.LoginMaster;
import canteen.sys.model.generic.HibernateDAOFactory;
import canteen.sys.view.ActionView;
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author TNT
 */
public class ViewBilling extends javax.swing.JInternalFrame implements ActionView<BillMaster> {
    
    HibernateDAOFactory hibernateDAOFactory = HibernateDAOFactory.getInstance();
    DefaultTableModel modelTableProcessed;
    DefaultTableModel modelTableProductList;
    List<BillMaster> listBill;
    EmployeeMaster em;

    /**
     * Creates new form ViewBilling
     */
    public ViewBilling() {
        initComponents();
        modelTableProcessed = (DefaultTableModel) tbBill.getModel();
        modelTableProductList = (DefaultTableModel) tbProduct.getModel();
        LoginMaster lm = SessionFactoryClass.getInstance().getUser();
        if (tbProduct.getRowCount() > 0) {
            modelTableProductList.setDataVector(null, new Object[]{"Product Name", "Product Type", "Qty", "Price"});
        }
        if (tbBill.getRowCount() > 0) {
            modelTableProcessed.setDataVector(null, new Object[]{"Id", "Create Date", "Total", "Status"});
        }
        if (lm != null) {
            listBill = hibernateDAOFactory.getBillMasterDAO().findAllBillSearchByUserId(lm.getlId());
            showList(listBill);
            listSelectionListenerPending();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbStatus = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        txtCouponDate = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtCouponNo = new javax.swing.JTextField();
        txtCreateDate = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        txtDepartment = new javax.swing.JTextField();
        txtEmployee = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtOrderId = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        labelStatus = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JLabel();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbProduct = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbBill = new javax.swing.JTable();

        setClosable(true);
        setTitle("Bill Viewer");

        cbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "Pending", "Processed", "Cancel" }));
        cbStatus.setToolTipText("");
        cbStatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbStatusActionPerformed(evt);
            }
        });

        jLabel1.setText("Status");

        jLayeredPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("View Details"));

        txtCouponDate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        txtCouponDate.setForeground(new java.awt.Color(0, 153, 153));
        txtCouponDate.setEnabled(false);
        txtCouponDate.setBounds(100, 200, 200, 20);
        jLayeredPane1.add(txtCouponDate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel9.setText("Coupon Date");
        jLabel9.setBounds(10, 200, 90, 20);
        jLayeredPane1.add(jLabel9, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel8.setText("Coupon No");
        jLabel8.setBounds(10, 170, 90, 20);
        jLayeredPane1.add(jLabel8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtCouponNo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        txtCouponNo.setForeground(new java.awt.Color(0, 153, 153));
        txtCouponNo.setEnabled(false);
        txtCouponNo.setBounds(100, 170, 200, 20);
        jLayeredPane1.add(txtCouponNo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtCreateDate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        txtCreateDate.setForeground(new java.awt.Color(0, 153, 153));
        txtCreateDate.setEnabled(false);
        txtCreateDate.setBounds(100, 100, 200, 20);
        jLayeredPane1.add(txtCreateDate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setText("Create Date");
        jLabel7.setBounds(10, 100, 90, 20);
        jLayeredPane1.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel6.setText("User Name");
        jLabel6.setBounds(10, 70, 90, 20);
        jLayeredPane1.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtUserName.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        txtUserName.setForeground(new java.awt.Color(0, 153, 153));
        txtUserName.setEnabled(false);
        txtUserName.setBounds(100, 70, 200, 20);
        jLayeredPane1.add(txtUserName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtDepartment.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        txtDepartment.setForeground(new java.awt.Color(0, 153, 153));
        txtDepartment.setEnabled(false);
        txtDepartment.setBounds(100, 250, 200, 20);
        jLayeredPane1.add(txtDepartment, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtEmployee.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        txtEmployee.setForeground(new java.awt.Color(0, 153, 153));
        txtEmployee.setEnabled(false);
        txtEmployee.setBounds(100, 280, 200, 20);
        jLayeredPane1.add(txtEmployee, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel13.setText("Status");
        jLabel13.setBounds(10, 130, 50, 20);
        jLayeredPane1.add(jLabel13, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel12.setText("Employee");
        jLabel12.setBounds(10, 280, 60, 20);
        jLayeredPane1.add(jLabel12, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel11.setText("Department");
        jLabel11.setBounds(10, 250, 70, 20);
        jLayeredPane1.add(jLabel11, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtOrderId.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        txtOrderId.setForeground(new java.awt.Color(0, 153, 153));
        txtOrderId.setEnabled(false);
        txtOrderId.setBounds(40, 40, 130, 20);
        jLayeredPane1.add(txtOrderId, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setText("ID");
        jLabel5.setBounds(10, 40, 11, 14);
        jLayeredPane1.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        labelStatus.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        labelStatus.setForeground(new java.awt.Color(0, 153, 153));
        labelStatus.setText("Pending");
        labelStatus.setBounds(100, 130, 200, 20);
        jLayeredPane1.add(labelStatus, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18)); // NOI18N
        jLabel2.setText("Total: ");
        jLabel2.setBounds(30, 370, 70, 30);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtTotal.setFont(new java.awt.Font("Tempus Sans ITC", 1, 24)); // NOI18N
        txtTotal.setForeground(new java.awt.Color(0, 204, 204));
        txtTotal.setText("$0");
        txtTotal.setBounds(110, 370, 210, 30);
        jLayeredPane1.add(txtTotal, javax.swing.JLayeredPane.DEFAULT_LAYER);

        tbProduct.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbProduct);

        jScrollPane1.setBounds(0, 190, 430, 220);
        jLayeredPane2.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        tbBill.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tbBill);

        jScrollPane2.setBounds(0, 0, 430, 180);
        jLayeredPane2.add(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
            .add(layout.createSequentialGroup()
                .add(jLayeredPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 432, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(cbStatus, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cbStatus, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                    .add(jLayeredPane2)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbStatusActionPerformed
        // TODO add your handling code here:
        showList(listBill);
    }//GEN-LAST:event_cbStatusActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbStatus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelStatus;
    private javax.swing.JTable tbBill;
    private javax.swing.JTable tbProduct;
    private javax.swing.JTextField txtCouponDate;
    private javax.swing.JTextField txtCouponNo;
    private javax.swing.JTextField txtCreateDate;
    private javax.swing.JTextField txtDepartment;
    private javax.swing.JTextField txtEmployee;
    private javax.swing.JTextField txtOrderId;
    private javax.swing.JLabel txtTotal;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables

    @Override
    public void deleteRow(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void updateRow(BillMaster row, int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void addRow(BillMaster row, boolean posion) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public final void showList(List<BillMaster> list) {
        String status;
        if (tbBill.getRowCount() > 0) {
            modelTableProcessed.setDataVector(null, new Object[]{"Id", "Create Date", "Total", "Status"});
        }
        for (BillMaster item : list) {
            double total = getTotalBill(item.getbId());
            if (cbStatus.getSelectedIndex() == 0) {
                if (item.getStatus() == 0) {
                    status = "pending";
                } else if (item.getStatus() == 1) {
                    status = "processed";
                    
                } else {
                    status = "cancel";
                }
                modelTableProcessed.insertRow(0, new Object[]{item.getbId(),
                            item.getbDate(), "$" + total, status});
            } else if (cbStatus.getSelectedIndex() == 1 && item.getStatus() == 0) {
                modelTableProcessed.insertRow(0, new Object[]{item.getbId(),
                            item.getbDate(), "$" + total, "pending"});
            } else if (cbStatus.getSelectedIndex() == 2 && item.getStatus() == 1) {
                modelTableProcessed.insertRow(0, new Object[]{item.getbId(),
                            item.getbDate(), "$" + total, "processed"});
            } else if (cbStatus.getSelectedIndex() == 3 && item.getStatus() == 2) {
                modelTableProcessed.insertRow(0, new Object[]{item.getbId(),
                            item.getbDate(), "$" + total, "cancel"});
            }
        }
    }
    
    @Override
    public void showDetail(BillMaster object) {
        try {
            txtOrderId.setText(object.getbId() + "");
            txtUserName.setText(object.getLoginMaster().getlName());
            txtCouponNo.setText(object.getCoupenNo() + "");
            txtCouponDate.setText(object.getCoupenDate() + "");
            txtCreateDate.setText(object.getbDate() + "");
            String status = null;
            if (object.getStatus() == 0) {
                status = "pending";
            }
            if (object.getStatus() == 1) {
                status = "processed";
            } else if (object.getStatus() == 2) {
                status = "cancel";
            }
            labelStatus.setText(status);
            txtTotal.setText(tbBill.getValueAt(tbBill.getSelectedRow(), 2).toString());
            List<BillDetail> listProduct = hibernateDAOFactory.getBillDetailDAO().findAllBillDetailByBillId(object.getbId());
            showListProduct(listProduct);
            if (object.getEmployeeMaster() != null) {
                txtEmployee.setText(object.getEmployeeMaster().geteFName() + " "
                        + object.getEmployeeMaster().geteMName() + " "
                        + object.getEmployeeMaster().geteLName());
            }
            txtDepartment.setText(object.getDepartment().getDepartmentName());
        } catch (NullPointerException ex) {
            txtEmployee.setText("");
            txtDepartment.setText("");
        }
        
    }
    
    @Override
    public BillMaster getObject(int index) {
        BillMaster bm = null;
        try {
            if (listBill != null && listBill.size() > 0) {
                bm = listBill.get(listBill.size() - 1 - index);
            }
        } catch (Exception ex) {
        }
        return bm;
    }
    
    @Override
    public void setObject(BillMaster object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public boolean validObject(BillMaster object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public final void listSelectionListenerPending() {
        tbBill.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int rowSelected = tbBill.getSelectedRow();
                showDetail(getObject(rowSelected));
            }
        });
    }
    
    private void showListProduct(List<BillDetail> listProduct) {
        if (tbProduct.getRowCount() > 0) {
            modelTableProductList.setDataVector(null, new Object[]{"Product Name", "Product Type", "Qty", "Price"});
        }
        for (BillDetail billDetail : listProduct) {
            modelTableProductList.insertRow(0, new Object[]{billDetail.getItem().getItemName(),
                        billDetail.getItem().getItemType().getItemTypeName(),
                        billDetail.getItemQty(), "$" + billDetail.getItemRate()});
        }
    }
    
    private double getTotalBill(int bId) {
        double total = 0;
        List<BillDetail> listBillDetail = hibernateDAOFactory.getBillDetailDAO().findAllBillDetailByBillId(bId);
        if (listBillDetail.size() > 0) {
            for (BillDetail billDetail : listBillDetail) {
                total += billDetail.getItemQty() * billDetail.getItemRate();
            }
        }
        return total;
    }
}
